"""
省级疫情可视化地图开发
"""
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts, TitleOpts

# 读取文件
file_path = "/Users/dongjian/Documents/学习/python-learn-资料/资料/第1-12章资料/资料/可视化案例数据/地图数据/疫情.txt"
f = open(file_path, "r", encoding="UTF-8")
read_data = f.read()
# 转换为json
json_data = json.loads(read_data)
# print(json_data)
# 获取河南省疫情数据
henan_data = json_data["areaTree"][0]["children"][3]["children"]

# 转换为地图数据
data_list = []
special_dict = {"济源示范区": "济源市"}
for city_data in henan_data:
    city_name = city_data["name"] + "市"
    if special_dict.get(city_data["name"]):
        city_name = special_dict.get(city_data["name"])
    city_confirm = city_data["total"]["confirm"]
    data_list.append((city_name, city_confirm))

# print(data_list)

# add方法添加地图数据
map = Map()
map.add("河南省疫情地图", data_list, "河南")
# 设置全局选项
map.set_global_opts(
    title_opts=TitleOpts(title="河南省疫情地图", pos_left="center", pos_bottom="1%"),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},
            {"min": 10, "max": 49, "label": "10-49人", "color": "#FFFF99"},
            {"min": 50, "max": 99, "label": "5-99人", "color": "#FF9966"},
            {"min": 100, "max": 149, "label": "100-149人", "color": "#FF6666"},
            {"min": 150, "max": 199, "label": "150-199人", "color": "#CC3333"},
            {"min": 200, "label": "200+人", "color": "#990033"}
        ]
    )
)
# 绘图
map.render("河南省疫情地图.html")
